// Package database @program: account
//@author: edte
//@create: 2021-12-28 15:24
package database

import (
	log2 "account/log"
	"account/tools"
	"context"
	"errors"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"strconv"
)

func AdminBlockDb(userId int) error {
	mysqlPath := tools.GetEnv("MYSQL_PATH", "localhost:3306")
	mysqlUser := tools.GetEnv("MYSQL_USER", "root")
	mysqlPassword := tools.GetEnv("MYSQL_PASSWORD", "123456")
	db, err := gorm.Open(mysql.Open(mysqlUser+":"+mysqlPassword+"@tcp("+mysqlPath+")/users?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})

	// db, err := gorm.Open(mysql.Open("root:123456@tcp(localhost:3306)/users?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return errors.New("failed to connect database")
	}
	tmpUser := tools.User{}
	db.Model(tools.User{}).Where("id = ?", userId).First(&tmpUser)
	if tmpUser.Id == 0 {
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Error", "Account does not exist.")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return errors.New("账号不存在")
	}
	if tmpUser.Status == true {
		db.Model(tools.User{}).Where("id = ?", tmpUser.Id).Take(&tmpUser)
		tmpUser.Status = false
		db.Model(tools.User{}).Where("id = ?", tmpUser.Id).Save(&tmpUser)
		db.Model(tools.User{}).Where("id = ?", userId).First(&tmpUser)
		if tmpUser.Status == true {
			writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Warring", "Unlock failure")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}

			return errors.New("Unlock failure")
		}
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Warring", "Unlock success")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return err
	}

	if tmpUser.Status == false {
		tmpUser.Status = true
		db.Model(&tools.User{}).Where("id = ?", tmpUser.Id).Updates(&tmpUser)
		db.Model(tools.User{}).Where("id = ?", userId).First(&tmpUser)
		if tmpUser.Status == false {

			writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Warring", "Banned failure")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}

			return errors.New("banned failure")
		}
		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Warring", "Banned success")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		return err
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockDb", strconv.Itoa(userId), "Error", "There is a strange error in blocking the account by the administrator.")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	return errors.New("there is a strange error in blocking the account by the administrator")
}
